/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.apache.http.HttpHeaders;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

/**
 * Swagger文档，只有在测试环境才会使用
 * @author LGH
 */
@Configuration
public class SwaggerConfiguration {

	@Bean
	public GroupedOpenApi baseRestApi() {
		return GroupedOpenApi.builder()
				.group("接口文档")
				.addOperationCustomizer((operation, handlerMethod) -> operation.addParametersItem(
						// 全局参数：token令牌, 解决doc.html中Authorize不生效问题，swagger-ui中可忽略此全局参数
						new HeaderParameter().name(HttpHeaders.AUTHORIZATION)
								.example("117455a7-2777-462e-8086-56fc96e4b5bf")
								.description("全局参数：token令牌, swagger-ui接口try-it-out中可不用关注此字段，在Authorize中设置即可")
								.schema(new StringSchema()
										.name(HttpHeaders.AUTHORIZATION)
										.description("token令牌"))))
				.packagesToScan("com.yami").build();
	}


	@Bean
	public OpenAPI springShopOpenApi() {
		return new OpenAPI()
				.info(new Info().title("Mall4j Web后台接口文档")
						.description("Mall4j接口文档，openapi3.0 接口，用于Web后台前端对接")
						.version("v0.0.1")
						.license(new License().name("使用请遵守AGPL3.0授权协议").url("https://www.mall4j.com")))
				.components(new Components().addSecuritySchemes(HttpHeaders.AUTHORIZATION, new SecurityScheme()
						.name(HttpHeaders.AUTHORIZATION)
						.type(SecurityScheme.Type.APIKEY)
						.description("token令牌")
						.in(SecurityScheme.In.HEADER)))
				.security(new ArrayList<>() {
					{
						add(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION));
					}
				});
	}
}
